const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    kill: [],
    hotlist: [],
    newlist: [],
    show: false,
    uid: '',
    notice: '',
    gonggao: [{
      title: '啦啦啦啦啊啊啊啊啊啊啊啊啊'
    }, {
      title: '啦啦啦啦啊啊啊啊啊啊啊啊啊'
    }, {
      title: '啦啦啦啦啊啊啊啊啊啊啊啊啊'
    }]
  },
  // 跳轮播
  jumpurl: function(e) {

    let that = this
    wx.getStorage({
      key: 'userinfo',
      success: function(res) {
        let src = e.currentTarget.dataset.url;
        wx.navigateTo({
          url: src
        })
      },
      fail: function(res) {
        wx.showModal({
          title: '提示',
          content: '需授权后才购买',
          confirmColor: '#5FA15F',
          // showCancel: false,
          confirmText: '前往授权',
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/login/welcome',
              })
            } else if (res.cancel) {

            }


          }
        })
      }
    })

  },
  // 跳系统消息
  jumpmessage() {
    wx.navigateTo({
      url: '/pages/message/message?uid=' + this.data.uid,
    })
  },
  // 跳公告
  jumpPublic() {



    let that = this
    wx.getStorage({
      key: 'userinfo',
      success: function(res) {

        wx.navigateTo({
          url: '/pages/message/public/public',
        })

      },
      fail: function(res) {
        wx.showModal({
          title: '提示',
          content: '需授权后才购买',
          confirmColor: '#5FA15F',
          // showCancel: false,
          confirmText: '前往授权',
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/login/welcome',
              })
            } else if (res.cancel) {

            }


          }
        })
      }
    })

  },
  // 跳搜索
  jumpsearch() {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  // 跳秒杀列表
  jumpmiao() {



    let that = this
    wx.getStorage({
      key: 'userinfo',
      success: function(res) {


        wx.navigateTo({
          url: '/pages/index/seckill/seckill',
        })


      },
      fail: function(res) {
        wx.showModal({
          title: '提示',
          content: '需授权后才购买',
          confirmColor: '#5FA15F',
          // showCancel: false,
          confirmText: '前往授权',
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/login/welcome',
              })
            } else if (res.cancel) {

            }


          }
        })
      }
    })


  },
  // 跳详情页
  jumpdetail: function(e) {

    let that = this
    wx.getStorage({
      key: 'userinfo',
      success: function(res) {
        console.log('缓存2', res)
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
          url: '/pages/goodsDetail/goodsDetail?id=' + id,
        })

      },
      fail: function(res) {
        wx.showModal({
          title: '提示',
          content: '需授权后才购买',
          confirmColor: '#5FA15F',
          // showCancel: false,
          confirmText: '前往授权',
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/login/welcome',
              })
            } else if (res.cancel) {

            }


          }
        })
      }
    })



  },
  // 跳秒杀详情
  jumpkill: function(e) {


    let that = this
    wx.getStorage({
      key: 'userinfo',
      success: function(res) {
        console.log('缓存2', res)
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
          url: '/pages/goodsDetail/seckillDetail/seckillDetail?id=' + id,
        })


      },
      fail: function(res) {
        wx.showModal({
          title: '提示',
          content: '需授权后才购买',
          confirmColor: '#5FA15F',
          // showCancel: false,
          confirmText: '前往授权',
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/login/welcome',
              })
            } else if (res.cancel) {

            }


          }
        })
      }
    })


  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    wx.getStorage({
      key: 'userinfo',
      success: function(res) {
        console.log('缓存', res.data)
        //公告一条
        wx.request({
          url: app.globalData.api + "getnoticelist",
          data: {
            typeid: 1
          },
          success: function(res) {
            console.log('公告', res)
            that.setData({
              notice: res.data.list
            })
          },
        })
        //轮播
        wx.request({
          url: app.globalData.api + "getbanner",
          data: {},
          success: function(res) {
            console.log('轮播', res)
            that.setData({
              imgUrls: res.data.list
            })
          },
        })
        wx.getStorage({
          key: 'userinfo',
          success: function(res) {
            console.log('缓存', res)
            that.setData({
              uid: res.data.id
            })
            console.log('uid', res.data.id)
            if (!res.data.id) {
              wx.navigateTo({
                url: '/pages/login/welcome',
              })
            }
            // 热门列表
            wx.request({
              url: app.globalData.api + "indexhitsgoods",
              data: {
                uid: res.data.id,
                level: res.data.level
              },
              success: function(res) {
                console.log(res)
                that.setData({
                  hotlist: res.data.list
                })
              },
            })
            //新品列表
            wx.request({
              url: app.globalData.api + "indexnewsgoods",
              data: {
                uid: res.data.id,
                // level: res.data.level
              },
              success: function(res) {
                console.log(res)
                let img = []
                img = res.data.list.img
                console.log(res.data.list)
                that.setData({
                  newlist: res.data.list
                })
              },
            })
          },
        })
        // 秒杀列表
        wx.request({
          url: app.globalData.api + "getspikelist",
          success: function(res) {
            console.log(res.data)
            that.setData({
              kill: res.data.list
            })
          },
        })

      },
      fail: function(res) {
        //公告一条
        wx.request({
          url: app.globalData.api + "getnoticelist",
          data: {
            typeid: 1
          },
          success: function(res) {
            console.log('公告', res)
            that.setData({
              notice: res.data.list
            })
          },
        })



        //轮播
        wx.request({
          url: app.globalData.api + "getbanner",
          data: {},
          success: function(res) {
            console.log('轮播', res)
            that.setData({
              imgUrls: res.data.list
            })
          },
        })
        // 热门列表
        wx.request({
          url: app.globalData.api + "indexhitsgoods",
          data: {
            uid: 0,
            // level: res.data.level
          },
          success: function(res) {
            console.log(res)
            that.setData({
              hotlist: res.data.list
            })
          },
        })
        //新品列表
        wx.request({
          url: app.globalData.api + "indexnewsgoods",
          data: {
            uid: 0,
            // level: res.data.level
          },
          success: function(res) {
            console.log(res)
            let img = []
            img = res.data.list.img
            console.log(res.data.list)
            that.setData({
              newlist: res.data.list
            })
          },
        })
        // wx.redirectTo({
        //   url: '/pages/login/welcome',
        // })
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this
    wx.getStorage({
      key: 'userinfo',
      success: function(res) {
        console.log('缓存2', res)
        //小红点状态
        wx.request({
          url: app.globalData.api + "getnoticemsg",
          data: {
            uid: res.data.id,
          },
          success: function(res) {
            console.log('小点点', res)
            if (res.data.code == 0) {
              that.setData({
                show: true
              })
            } else {
              that.setData({
                show: false
              })
            }
          },
        })

      },
    })



  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})